package com.zm.zmgz.controller;


import com.zm.zmgz.annotationzm.ButtonAuthorityAnnotation;
import com.zm.zmgz.annotationzm.LoginRequired;
import com.zm.zmgz.pojo.*;
import com.zm.zmgz.service.EmployeeService;
import com.zm.zmgz.service.ShowEmployeeService;
import com.zm.zmgz.utils.ControllerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/employees")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService; // employeezm表相关服务

    @Autowired
    ShowEmployeeService showEmployeeService;


    /***
    * @Description: 导入excel
    * @Param:-
     * 步骤:检查
     *        是否为空,或长度为0
     *        检查姓名,身份证,联系电话,状态是否为空(0,1,2)
     *     事务内
     *        根据主键删除
     *        根据主键新增
     *     返回Boolean，及提示信息
    * @return:
    * @Author: cjw
    * @Date: 2020-04-20 14:17:42
    */
    @LoginRequired // 需要登录
    @PutMapping("/importExcel")
    @Transactional
    public Response importExcel(@RequestHeader("X-Token") String token,@RequestBody List<EmployeeBean> beanList){
       if(Objects.isNull(beanList) || beanList.size() == 0) {
           return new Response(20000,"导入数据为空或数据错误,请检查导入文件!",false);
       }
        System.err.println(beanList);
        for (EmployeeBean employeeBean : beanList) { // 检查姓名,身份证,联系电话,状态是否为空(0,1,2)
            if(Objects.isNull(employeeBean.getName())) {
                return new Response(20000,"姓名不能为空!",false);
            }
            if(Objects.isNull(employeeBean.getIdentificationnumber())) {
                return new Response(20000,"身份证不能为空!",false);
            }
            if(Objects.isNull(employeeBean.getPhone())) {
                return new Response(20000,"手机号不能为空!",false);
            }
            if(Objects.isNull(employeeBean.getStatus())) {
                return new Response(20000,"状态不能为空!",false);
            }
            if(!(employeeBean.getStatus().equals("0")  || employeeBean.getStatus().equals("1") || employeeBean.getStatus().equals("2"))) {
                return new Response(20000,"状态可选值(试用,转正,离职)!",false);
            }
        }
        for (EmployeeBean employeeBean : beanList) {
            employeeService.deleteEmployee(employeeBean);
        }
        for (EmployeeBean employeeBean : beanList) {
            employeeService.addEmployee(employeeBean);
        }

        return new Response(20000,"导入成功!",true);
    }

    /*
    * 功能:返回员工信息数据(数据包括:完整列表,精简列表,隐藏列表,employee显示信息)
    * 步骤:1.显示模式并处理
    *           获取:获取token(用户名)->用户对象->用户身份证->useremployeezm表->完整列表,精简列表,隐藏列表(如果不存在就新建该条记录)
    *           获取一个usermployeezm对象list对象,理想其中应该有3个对象,不存在就新建记录,在获取.
    *           把每一个对象的showorder处理为一个list集合,并以complete,streamline,hide为键值添加到data对象(map对象)中
    *     2.搜索表格记录处理并返回
    *           获取employeezm所有记录(分页:第几页(page),每页多少记录(limit);排序:状态(先非离职在离职,非离职日期降序,离职日期降序))
    *
    *
    * */
    @LoginRequired // 需要登录
    @GetMapping("/getemployees")
    public Response getEmployees(@RequestHeader("X-Token") String token, ShowEmployeesParamBean employeesParamBean, HttpServletRequest request){
        String identificationNumber =  ControllerUtils.getIdentificationNumber(token,request); // 获取身份证
        Map displayEmployees = showEmployeeService.getDisplayModes(identificationNumber); // 获取显示模式信息
        employeesParamBean.setlimitAAndlimitB();
        System.out.println(employeesParamBean.toString());
        if(employeesParamBean.getSearchOptionValue() == "")
            employeesParamBean.setSearchOptionValue(null);
        displayEmployees = employeeService.getEmployees(displayEmployees,employeesParamBean); // 获取员工信息
        return new Response(20000,"获取员工信息成功!",displayEmployees);
    }

    /*
    * 功能:select的可能值为:姓名|身份证|推荐人;与EmployeeBean对象的name,idcard,rname属性对应,从数据库中查找,去重返回list<string> json对象即可
    *
    * */
    @LoginRequired
    @GetMapping("/getQueryPrompts")
    public Response getQueryPrompts(String searchOption){
        List<EmployeeQueryPromptsBean> list = employeeService.getIdOrNameOrRname(searchOption);
        return new Response(20000,"获取搜索推荐值成功!",list);
    }

    /*
     * 功能:验证该身份证是否存在,如果存在返回1不是存在,返回0表示不存在
     *
     * */
    @LoginRequired
    @GetMapping("/validateIdentificationnumber")
    public Response validateIdentificationnumber(@RequestHeader("X-Token") String token, String identificationnumber){
        int count = employeeService.validateIdentificationnumber(identificationnumber);
        return count == 1 ? new Response(20000,"该身份证已经被注册!",count) : new Response(20000,"该身份证可以使用!",count);
    }

    /*
     * 功能:增加1个员工
     *
     * */
    @LoginRequired
    @PostMapping("/addEmployee")
    @ButtonAuthorityAnnotation("/employee/employeeInformation/add")
    public Response addEmployee(@RequestHeader("X-Token") String token,@RequestBody EmployeeBean employeeBean){
        int count = employeeService.addEmployee(employeeBean);
        return count == 1 ? new Response(20000,"员工添加成功!",count) : new Response(20000,"员工添加失败!",count);
    }

    /*
     * 功能:修改1个员工的信息
     *
     * */
    @LoginRequired
    @PutMapping("/editEmployee")
    @ButtonAuthorityAnnotation("/employee/employeeInformation/edit")
    public Response editEmployee(@RequestHeader("X-Token") String token,@RequestBody EmployeeBean employeeBean){
        System.out.println("employeeBean:"+employeeBean.toString());
        int count = employeeService.editEmployee(employeeBean);
        return count == 1 ? new Response(20000,"员工信息修改成功!",count) : new Response(20000,"员工信息修改失败!",count);
    }

    /*
     * 功能:删除1个员工
     *
     * */
    @LoginRequired
    @DeleteMapping("/deleteEmployee")
    @ButtonAuthorityAnnotation("/employee/employeeInformation/delete")
    public Response deleteEmployee(@RequestHeader("X-Token") String token,@RequestBody EmployeeBean employeeBean){
        System.out.println("employeeBean:"+employeeBean.toString());
        int count = employeeService.deleteEmployee(employeeBean);
        return count == 1 ? new Response(20000,"员工删除成功!",count) : new Response(20000,"员工删除失败!",count);
    }

    /*
     * 功能:获取显示模式信息;返回数据格式:map<string,list<map<string,string>>>
     *
     * */
    @LoginRequired
    @GetMapping("/getDisployData")
    public Response getDisployData(@RequestHeader("X-Token") String token,HttpServletRequest request){
        ServletContext servletContext = request.getServletContext();
        UserBean userBean = (UserBean)servletContext.getAttribute(request.getSession().getId());
        Map<String,List<Map<String,String>>> map = showEmployeeService.getDisployData(userBean.getIdentificationnumber()); // 获取显示模式信息
        return map.size() > 0 ? new Response(20000,"显示模式信息获取成功!",map) : new Response(20000,"显示模式信息获取失败!",map); // 返回结果集
    }


    /*
     * 功能:设置显示模式信息
     *
     * */
    @LoginRequired
    @PutMapping("/setDisployData")
    public Response setDisployData(@RequestHeader("X-Token") String token,HttpServletRequest request,@RequestBody DisployDataBean disployDataBean){
//      System.out.println("DisployDataBean:"+disployDataBean.toString());
        ServletContext servletContext = request.getServletContext();
        UserBean userBean = (UserBean)servletContext.getAttribute(request.getSession().getId());
        int count = showEmployeeService.setDisployData(userBean.getIdentificationnumber(),disployDataBean); // 获取显示模式信息
        return count > 0 ? new Response(20000,"显示模式信息修改成功!",count) : new Response(20000,"显示模式信息没有修改!",count); // 返回结果集
    }

    /*
     * 功能:测试
     *
     * */
    @GetMapping("/test")
    public EmployeeBean test(String identificationNumber){
        return employeeService.test(identificationNumber);
    }


}
